package com.example.gsq.news.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.gsq.news.R;
import com.example.gsq.news.base.BaseActivity;

import static android.view.KeyEvent.KEYCODE_BACK;

public class NewsContentActivity extends BaseActivity {

    private WebView webView;

    private LinearLayout noNetLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_content);
        noNetLayout = (LinearLayout) findViewById(R.id.layout_nonet);
        webView = (WebView) findViewById(R.id.web_view);
        final String contentUrl = getIntent().getStringExtra("data");
        webView.setWebViewClient(new WebViewClient());
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        if(BaseActivity.isNetworkAvailable(this)){
            webView.loadUrl(contentUrl);
        } else {
            noNetLayout.setVisibility(View.VISIBLE);
        }
        noNetLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(BaseActivity.isNetworkAvailable(NewsContentActivity.this)){
                    webView.loadUrl(contentUrl);
                    noNetLayout.setVisibility(View.GONE);
                } else {
                    Toast.makeText(NewsContentActivity.this, "网络连接不可用，请稍后重试", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
